﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;


namespace Bombif.Game
{
    public partial class GameMain
    {
        public string GetLocalIPAddress()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();
                }
            }
            return localIP;
        }

        private void startSend_Socket(Socket socket)
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPAddress ip_multicast = IPAddress.Parse("224.5.6.7");
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip_multicast));
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 2);
            IPEndPoint ipep = new IPEndPoint(ip_multicast, 4055);
            socket.Connect(ipep);
        }

        private void startReceive_Socket(Socket socket)
        {
            socket =new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp);

            IPEndPoint ipep=new IPEndPoint(IPAddress.Any, 4056);
            socket.Bind(ipep);

            IPAddress ip=IPAddress.Parse("224.5.6.7");

            socket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.AddMembership,new MulticastOption(ip,IPAddress.Any));
        }
    }
}
